import axios from 'axios'
import qs from 'qs'

export function request(url, params) {
  const instance = axios.create({
    baseURL: 'http://localhost:8080',
    timeout: 30000,
    // withCredentials: true
  });

  instance.interceptors.request.use(config => {
    // console.log("请求配置拦截");
    if (config.method === 'get') {
      config.paramsSerializer = function(params) {
        return qs.stringify(params, { arrayFormat: 'repeat' })
      }
    }
    return config;
  }, error => {
    console.log(error);
  });

  instance.interceptors.response.use(result => {
    // console.log(result);
    return result;
  }, error => {
    console.log(error);

  });

  // instance.defaults.withCredentials = true; 涉及跨域
  if (!params) {
    return instance.get(url);
  } else {
    params = {
      params: params
    };
    return instance.get(url, params);
  }
}

